<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>属性模板列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
<!--  :row-class-name="tableRowClassName"  -->
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<!--   id, name, pinyin, keywords, sort   -->
      <el-table-column prop="id" label="ID" align="center" width="180"></el-table-column>
      <el-table-column prop="name" label="属性模板名称" align="center" width="180"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" align="center" width="180"></el-table-column>
      <el-table-column prop="keywords" label="关键词" align="center" width="180"></el-table-column>
      <el-table-column prop="sort" label="排序序号" ></el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openHandleDelete(scope.row)" ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "AttributeTemplateListView",
  data() {
    return {
      tableData: []
    }
  },
  created(){
    console.log("created...")
  },
  mounted() {
    console.log("mounted...")
    this.loadAttributeTemplateList()
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    loadAttributeTemplateList(){
      console.log("loadAttributeTemplateList...")
      let url = "http://localhost:9080/attribute-templates"
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("response参数：")
        console.log(response)
        let responseBody = response.data;
        console.log("response的data参数：")
        console.log(responseBody)
        this.tableData = responseBody.data
      })
    },
    openHandleDelete(attributeTemplate) {
      this.$confirm('是否删除【'+attributeTemplate.name+'(id='+attributeTemplate.id+')】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.handleDelete(attributeTemplate)
      }).catch(() => {
      });
    },
    handleDelete(attributeTemplate){
      // alert("你点击了【"+name+"(id="+id+")】的删除按钮")
      let url = 'http://localhost:9080/attribute-templates/'+attributeTemplate.id+'/delete'
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        if(responseBody.state != 20000){
          this.$message.error("删除失败")
        }
        this.loadAttributeTemplateList()
      })
    },
    handleEdit(attributeTemplate){
      alert("你点击了【"+attributeTemplate.name+"(id="+attributeTemplate.id+")】的编辑按钮")
    }
  }
}
</script>

<style scoped>

</style>